// 20
#pragma once

#include "noncopyable.h"
#include <vector>
#include <unordered_map>
#include "Timestamp.h"

class Channel;
class EventLoop;
// 抽象类
// muduo库中多路事件分发器的核心IO复用模块
class Poller : noncopyable {
    public:
        using ChannelList = std::vector<Channel*>;

        Poller(EventLoop *loop);
        virtual ~Poller() = default;

        // 给所有的IO复用提供统一的接口
        virtual Timestamp poll(int timeoutMs, ChannelList* activeChannels) = 0;
        virtual void updateChannel(Channel* channel) = 0;
        virtual void removeChannel(Channel* channel) = 0;

        // 判断channel是否在当前Poller中
        virtual bool hasChannel(Channel* channel) const;

        // EventLoop可以通过该接口获取默认的IO复用的具体实现
        static Poller* newDefaultPoller(EventLoop* loop);

    protected:
        // key: sockfd  value: sockfd所属的channel通道类型
        using ChannelMap = std::unordered_map<int, Channel*>;
        ChannelMap channels_;
    private:
        EventLoop* ownerLoop_;
    
};